﻿namespace Simnova.Collaborate.WPFApplication
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Simnova.Collaborate.Presenter;
    using Simnova.Collaborate.View;
    using System.Configuration;

    /// <summary>
    /// Interaction logic for LoginForm.xaml
    /// </summary>
    public partial class LoginForm : Window, ILogOn 
    {
        private LogOnPresenter _loginPresenter;

        public LoginForm()
        {
            InitializeComponent();
        }

        #region ILogin Members

        public string EmailAddress
        {
            get
            {
                return emailAddressTextBox.Text;
            }

            set
            {
                emailAddressTextBox.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return passwordBox.Password;
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this._loginPresenter = new Simnova.Collaborate.Presenter.LogOnPresenter(this);

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ClientSettingsSection css = (ClientSettingsSection)config.SectionGroups["userSettings"].Sections[0];
            System.Configuration.SettingElement se = css.Settings.Get("User");
            emailAddressTextBox.Text = ((SettingValueElement)se.Value).ValueXml.InnerText; 
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            int userId = 0;
            try
            {
               // loginErrorProvider.Clear();
                userId = this._loginPresenter.LogOn();
            }
            catch (Exception ex)
            {
                if (ex.Message == "Login Failed")
                {
                   // loginErrorProvider.SetError(loginButton, "Login Failed");
                }
                else
                {
                    throw;
                }

                return;
            }

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ClientSettingsSection css = (ClientSettingsSection)config.SectionGroups["userSettings"].Sections[0];

            ////System.Configuration.ClientSettingsSection css = new System.Configuration.ClientSettingsSection();

            System.Configuration.SettingElement se = css.Settings.Get("User"); // new System.Configuration.SettingElement("username",System.Configuration.SettingsSerializeAs.String);
            ((SettingValueElement)se.Value).ValueXml.InnerText = emailAddressTextBox.Text;

            config.Save(ConfigurationSaveMode.Modified, true);

            TimeLog timeLog = new TimeLog(userId);
            timeLog.Show();
            this.Hide();
        }
    }
}
